package com.chaofan.sell.controller;

import com.chaofan.sell.entity.ProductCategory;
import com.chaofan.sell.entity.ProductInfo;
import com.chaofan.sell.exception.SellException;
import com.chaofan.sell.form.ProductForm;
import com.chaofan.sell.service.CategoryService;
import com.chaofan.sell.service.ProductService;
import com.chaofan.sell.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 卖家端商品
 *
 * @author Fairy
 * @date 2021/10/15
 */
@Controller
@RequestMapping("/seller/product")
@Slf4j
public class SellerProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 卖家端商品列表
     *
     * @param page 分页
     * @param size 单页大小
     * @return model and view
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map) {
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        Page<ProductInfo> productInfoPage = productService.findAll(pageRequest);
        map.put("productInfoPage", productInfoPage);
        map.put("currentPage", page);
        map.put("size", size);
        log.info("【卖家端】查询商品列表");
        return new ModelAndView("product/list", map);
    }

    /**
     * 商品上架
     *
     * @param productId 待上架商品id
     * @return model and view
     */
    @GetMapping("/on_sale")
    public ModelAndView onSale(@RequestParam("productId") String productId, Map<String, Object> map) {
        try {
            productService.onSale(productId);
        } catch (SellException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/product/list");
            return new ModelAndView("error", map);
        }
        map.put("msg", "上架成功");
        map.put("url", "/sell/seller/product/list");
        return new ModelAndView("success", map);
    }

    /**
     * 商品下架
     *
     * @param productId 待下架商品id
     * @return model and view
     */
    @GetMapping("/off_sale")
    public ModelAndView offSale(@RequestParam("productId") String productId, Map<String, Object> map) {
        try {
            productService.offSale(productId);
        } catch (SellException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/product/list");
            return new ModelAndView("error", map);
        }
        map.put("msg", "下架成功");
        map.put("url", "/sell/seller/product/list");
        return new ModelAndView("success", map);
    }

    /**
     * 修改或新增商品界面
     *
     * @param productId 选填商品id
     * @return ModelAndView
     */
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "productId", required = false) String productId, Map<String, Object> map) {
        if (!StringUtils.isEmpty(productId)) {
            ProductInfo productInfo = productService.findOne(productId);
            map.put("productInfo", productInfo);
        }
        //查询类型
        List<ProductCategory> productCategoryList = categoryService.findAll();
        map.put("categoryList", productCategoryList);

        return new ModelAndView("product/index", map);
    }

    /**
     * 保存/新增商品
     *
     * @param form          商品信息表单
     * @param bindingResult 绑定结果
     * @return ModelAndView
     */
    //@CachePut(cacheNames = "product", key = "123")
    //@CacheEvict(cacheNames = "product", key = "123")
    @PostMapping("/save")
    public ModelAndView save(@Valid ProductForm form, BindingResult bindingResult, Map<String, Object> map) {
        if (bindingResult.hasErrors()) {
            map.put("msg", Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
            map.put("url", "/sell/seller/product/index");
            return new ModelAndView("error", map);
        }
        try {
            ProductInfo product = new ProductInfo();
            if (StringUtils.isNotEmpty(form.getProductId())) {
                product = productService.findOne(form.getProductId());
            } else {
                form.setProductId(KeyUtil.getUniqueKey());
            }
            BeanUtils.copyProperties(form, product);
            productService.save(product);
        } catch (SellException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/product/index");
            return new ModelAndView("error", map);
        }
        map.put("url", "/sell/seller/product/list");
        return new ModelAndView("success", map);
    }

}
